package com.spider.moudle.task;

import com.alibaba.fastjson.JSONObject;
import com.spider.moudle.api.Downloader;
import com.spider.moudle.constant.Constant;
import com.spider.moudle.entity.ResponseEntity;
import com.spider.moudle.entity.Task;
import com.spider.moudle.factory.DownloaderFactory;
import com.spider.moudle.util.CommonUtil;
import com.spider.moudle.util.ErrorLogUtil;
import com.spider.moudle.util.PropertiesUtil;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Linbo Hu on 2017/12/7.
 * 查询用户主页是否存在
 */
public class TaskQuery {

    /**
     * 言论任务检查池
     */
    private TaskChecker checker = TaskChecker.getInstance();

    /**
     * 发送请求的downloader
     */
    private Downloader downloader = DownloaderFactory.buildDownloader();


    private String queryMethod = PropertiesUtil.getVal("task.query.method");

    /**
     * 判断某个用户key是否已经抓过
     *
     * @param task 任务
     * @return
     */
    public Boolean userQuery(Task task) {
        Boolean isExist = null;
        if ("LOCAL".equals(queryMethod)) {
            isExist = checker.contains(task);
        } else if ("REMOTE".equals(queryMethod)) {
            String url = "http://" + PropertiesUtil.getVal("hbase.remote.query") + "/check/checkUserKeyForRedis";
            Map<String, String> map = new HashMap<>();
            String userKey = task.getTaskKey();
            map.put("userkey", userKey);
            ResponseEntity result = null;
            String body = null;
            try {
                result = downloader.post(url, map,Constant.USER_QUERY_TIMEOUT);
                body = result.getResponse_body();
            } catch (Exception e) {
                ErrorLogUtil.error("userkey: " + userKey + "查询失败");
                return null;
            }
            if (body == null) {
                return null;
            }
            JSONObject resutObject = JSONObject.parseObject(body);
            Integer i = resutObject.getInteger("status");
            if (i == 1) {
                isExist = true;
            } else if (i == -1) {
                isExist = false;
            }
        } else {
            System.err.println("The value of method must be LOCAL or REMOTE");
        }
        return isExist;
    }

    /**
     * 查询某个言论url是否被抓取过
     * @param task 任务
     * @return
     */
    public Boolean speechQuery(Task task) {
        String speechkey = task.getTaskKey();
        if(CommonUtil.isNull(speechkey)) {
            System.err.println("the value of speechkey is empty");
        }
        Boolean isExist = null;
        if ("LOCAL".equals(queryMethod)) {
            isExist = checker.contains(task);
        } else if ("REMOTE".equals(queryMethod)) {
            String url = "http://" + PropertiesUtil.getVal("hbase.remote.query") + "/check/checkSpeechForRedis";
            Map<String, String> map = new HashMap<>();
            map.put("speechkey", speechkey);
            ResponseEntity result = null;
            String body = null;
            try {
                result = downloader.post(url, map,Constant.USER_QUERY_TIMEOUT);
                body = result.getResponse_body();
            } catch (Exception e) {
                ErrorLogUtil.error("speechkey: " + speechkey + "查询失败");
                return null;
            }
            if (body == null) {
                return null;
            }
            JSONObject resutObject = JSONObject.parseObject(body);
            Integer i = resutObject.getInteger("status");
            if (i == 1) {
                isExist = true;
            } else if (i == -1) {
                isExist = false;
            }
        } else {
            System.err.println("The value of method must be LOCAL or REMOTE");
        }
        return isExist;
    }



}
